import time

import unittest
import logging
import requests
from parameterized import parameterized

from api.regLoginApi import RegLoginApi
from common.common_assert import common_assert, common_assert1
from common.mysql import clear_user_info
from common.read_json import read_json


class TestRegLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        """
        功能：类前置
        """
        clear_user_info()

    def setUp(self) -> None:
        """
        功能：方法前置
        """
        self.session = requests.session()
        self.reg_login_api = RegLoginApi(self.session)

    def tearDown(self) -> None:
        """
        功能：方法后置
        """
        self.session.close()

    # 获取图片验证码测试用例

    @parameterized.expand(read_json('test_data', 'img_verify_code'))
    def test01_img_verify_code(self, case_name, r, exp_status_code):
        # 打印接口测试用例名称
        print(case_name)
        logging.info(case_name)
        res = self.reg_login_api.get_img_verify_code(r)
        self.assertEqual(exp_status_code, res.status_code)

    # 获取短信验证码测试用例
    @parameterized.expand(read_json('test_data', 'phone_verify_code'))
    def test02_phone_verify_code(self, case_name, body, exp_status_code, exp_status, description):
        # 打印接口测试用例名称
        print(case_name)
        logging.info(case_name)
        self.reg_login_api.get_img_verify_code('123')
        res = self.reg_login_api.get_phone_verify_code(body)
        common_assert(self, res, exp_status_code, exp_status, description)

        # 注册测试用例

    @parameterized.expand(read_json('test_data', 'user_register'))
    def test03_register(self, case_name, body, exp_status_code, exp_status, description):
        # 打印接口测试用例名称
        print(case_name)
        logging.info(case_name)
        self.reg_login_api.get_img_verify_code('1234')
        self.reg_login_api.get_phone_verify_code(body={"phone": body.get("phone"),
                                                       "imgVerifyCode": "8888",
                                                       "type": "reg"})
        res = self.reg_login_api.user_register(body)
        common_assert1(self, res, exp_status_code, exp_status, description,case_name)

        # self.assertEqual(200, res.status_code)
        # self.assertEqual(200, res_dict['status'])
        # self.assertIn('注册成功', res_dict['description'])

    # 登录测试用例
    @parameterized.expand(read_json('test_data', 'user_login'))
    def test04_user_login(self, case_name, body, exp_status_code, exp_status, description):
        # 打印接口测试用例名称
        print(case_name)
        logging.info(case_name)
        res = self.reg_login_api.user_login(body)

        common_assert1(self, res, exp_status_code, exp_status, description,case_name)

        # res_dict = res.json()

        # self.assertEqual(200, res.status_code)
        # self.assertEqual(200, res_dict['status'])
        # self.assertIn('登录成功', res_dict['description'])

    @unittest.skip
    def test05_user_login(self):
        # 第一次错误
        body = {
            "keywords": "18708309008",
            "password": "1234"
        }

        # 调研封装接口请的方法，获取响应结果。
        res = self.reg_login_api.user_login(body)
        # 断言--调用公共断言方法
        common_assert(self, res, 200, 100, "密码错误1次")

        body = {
            "keywords": "18708309008",
            "password": "12345"
        }

        # 调研封装接口请的方法，获取响应结果。
        res = self.reg_login_api.user_login(body)
        # 断言--调用公共断言方法
        common_assert(self, res, 200, 100, "密码错误2次")

        body = {
            "keywords": "18708309008",
            "password": "123456"
        }

        # 调研封装接口请的方法，获取响应结果。
        res = self.reg_login_api.user_login(body)
        # 断言--调用公共断言方法
        common_assert(self, res, 200, 100, "由于连续输入错误密码达到上限")
        time.sleep(60)
        body = {
            "keywords": "18708309008",
            "password": "123456a"
        }

        # 调研封装接口请的方法，获取响应结果。
        res = self.reg_login_api.user_login(body)
        # 断言--调用公共断言方法
        common_assert(self, res, 200, 200, "登录成功")
